<?php
/**
 * Created by PhpStorm.
 * User: L
 * Date: 20.10.30
 * Time: 17:31
 */

namespace App;


use App\Models\Server;

class BtApi implements FileManege
{
    private $BT_KEY = "";  //接口密钥
    private $BT_PANEL = "";       //面板地址

    //如果希望多台面板，可以在实例化对象时，将面板地址与密钥传入
    public function __construct($bt_panel = null,$bt_key = null){
        if($bt_panel) $this->BT_PANEL = $bt_panel;
        if($bt_key) $this->BT_KEY = $bt_key;
    }

    public function check()
    {
        $data = $this->GetSystemTotal();
        return ['res'=>0,'data'=>$data];
    }

    public function getInfo(){
        return [$this->BT_PANEL,$this->BT_KEY];
    }

    public function GetSystemTotal(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/system?action=GetSystemTotal';

        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
//        $p_data['table'] = 'logs';
//        $p_data['limit'] = 10;
//        $p_data['tojs'] = 'test';

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);

        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

//    public function GetPHPVersion(){
//        //拼接URL地址
//        $url = $this->BT_PANEL.'/site?action=GetPHPVersion';
//
//        //准备POST数据
//        $p_data = $this->GetKeyData();      //取签名
////        $p_data['table'] = 'logs';
////        $p_data['limit'] = 10;
////        $p_data['tojs'] = 'test';
//
//        //请求面板接口
//        $result = $this->HttpPostCookie($url,$p_data);
//
//        //解析JSON数据
//        $data = json_decode($result,true);
//        return $data;
//    }


//    public function AddSite(){
//        //拼接URL地址
//        $url = $this->BT_PANEL.'/site?action=AddSite';
//
//        //准备POST数据
//        $p_data = $this->GetKeyData();      //取签名
//        $p_data['webname_1'] = 't.cd.phasercn.com';
//        $p_data['ps'] = 't.cd.phasercn.com';
//        $p_data['path'] = '/www/wwwroot/t.cd.phasercn.com';
//        $p_data['datauser'] = 't_cd_phasercn';
//        $p_data['datapassword'] = 'aNXKeZr6ae';
//        $p_data['version'] = 72;
//        $p_data['port'] = 80;
//        $p_data['webname'] = '{"domain":"t.cd.phasercn.com","domainlist":[],"count":1}';
//        $p_data['ftp'] = false;
//        $p_data['sql'] = true;
//        $p_data['address'] = 'localhost';
//        $p_data['codeing'] = 'utf8';
//
//        //请求面板接口
//        $result = $this->HttpPostCookie($url,$p_data);
//
//        //解析JSON数据
//        $data = json_decode($result,true);
//        return $data;
//    }
//    public function SetupPackage(){
//        //拼接URL地址
//        $url = $this->BT_PANEL.'/deployment?action=SetupPackage';
//
//        //准备POST数据
//        $p_data = $this->GetKeyData();      //取签名
//        $p_data['dname'] = 'wordpress';
//        $p_data['site_name'] = 't.cd.phasercn.com';
//        $p_data['php_version'] = 72;
//
//        //请求面板接口
//        $result = $this->HttpPostCookie($url,$p_data);
//
//        //解析JSON数据
//        $data = json_decode($result,true);
//        return $data;
//    }

    public function DeleteSite(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=DeleteSite';

        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id'] = '40';
        $p_data['webname'] = 't.cd.phasercn.com';
        $p_data['ftp'] = 1;
        $p_data['database'] = 1;
        $p_data['path'] = 1;

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);

        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

    //示例取面板日志
    public function GetLogs(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=getData';

        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['table'] = 'logs';
        $p_data['limit'] = 10;
//        $p_data['tojs'] = 'test';

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);

        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

    public function GetSites(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=getData';

        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['table'] = 'sites';
        $p_data['limit'] = 10;
//        $p_data['tojs'] = 'test';

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);

        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function SiteStop(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=SiteStop';

        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['table'] = 'sites';
        $p_data['limit'] = 10;
//        $p_data['tojs'] = 'test';

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);

        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }


    /**
     * 构造带有签名的关联数组
     */
    private function GetKeyData(){
        $now_time = time();
        $p_data = array(
            'request_token' =>  md5($now_time.''.md5($this->BT_KEY)),
            'request_time'  =>  $now_time
        );
        return $p_data;
    }


    /**
     * 发起POST请求
     * @param String $url 目标网填，带http://
     * @param Array|String $data 欲提交的数据
     * @return string
     */
    private function HttpPostCookie($url, $data,$timeout = 60)
    {
        //定义cookie保存位置
        $cookie_file='./'.md5($this->BT_PANEL).'.cookie';
        if(!file_exists($cookie_file)){
            $fp = fopen($cookie_file,'w+');
            fclose($fp);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    public function GetDiskInfo(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/system?action=GetDiskInfo';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function GetNetWork(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/system?action=GetNetWork';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function GetTaskCount(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/ajax?action=GetTaskCount';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function UpdatePanel(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/ajax?action=UpdatePanel';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['check'] = 'true';
        $p_data['force'] = 'false';

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function webList(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=getData&table=sites';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        // $p_data['p'] = 1;
        $p_data['limit'] = 15;


        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function webCategory(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=get_site_types';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名

        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function stopWeb(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=SiteStop';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id']=23;
        $p_data['name']='t.cd.phasercn.com';
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function startWeb(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=SiteStart';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id']=23;
        $p_data['name']='t.cd.phasercn.com';
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function SetEdate(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=SetEdate';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id']=23;
        $p_data['edate']=' 2020-01-01';
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function setPs(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=setPs&table=sites';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id']=23;
        $p_data['ps']='哈哈';
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function getData(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=getData&table=domain';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['search']=23;
        $p_data['list']='true';
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function AddDomain(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=AddDomain';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id']=23;
        $p_data['webname']='haha';
        $p_data['domain']='www.haha.com';
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function DelDomain(){
        //拼接URL地址
        $url = $this->BT_PANEL.'/site?action=DelDomain';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['id']=23;
        $p_data['webname']='haha';
        $p_data['domain']='www.haha.com';
        $p_data['port']=80;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }


    public function GetFileBody(string $path){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=GetFileBody';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['path']=$path;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

    public function SaveFileBody(string $path,string $data,Server $server=null){
        $this->CreateFile($path);

        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=SaveFileBody';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['path']=$path;
        $p_data['encoding']='utf-8';
        $p_data['data']=$data;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

    public function GetDir(string $path){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=GetDir&tojs=GetFiles&p=1&showRow=200';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['path']=$path;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

    public function GetDirList(string $path,string $search){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=GetDirList';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['p']=1;
        $p_data['showRow']=100;
        $p_data['path']=$path;
        $p_data['search']=$search;
        $p_data['all']=true;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function CreateDir(string $path){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=CreateDir';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['path']=$path;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function MvFile(string $path,string $nameOld,string $nameNew){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=MvFile';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['sfile']=$path.$nameOld;
        $p_data['dfile']=$path.$nameNew;
        $p_data['rename']=true;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function CreateFile(string $path){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=CreateFile';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['path']=$path;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }
    public function DeleteFile(string $path){
        //拼接URL地址
        $url = $this->BT_PANEL.'/files?action=DeleteFile';
        //准备POST数据
        $p_data = $this->GetKeyData();      //取签名
        $p_data['path']=$path;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }


}
